/*!
    \copyright  Copyright (c) 2021 - 2024 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    \ingroup    gatt_server_dis
    \brief
*/

#include "gatt_server_dis_advertising.h"

#include "le_advertising_manager.h"
#include <gatt.h>
#include <panic.h>

#define SIZE_DEVICE_INFO_ADVERT             4

static const uint8 gatt_device_info_advert_data[SIZE_DEVICE_INFO_ADVERT] = { \
    SIZE_DEVICE_INFO_ADVERT - 1, \
    ble_ad_type_complete_uuid16, \
    GATT_SERVICE_UUID_DEVICE_INFORMATION & 0xFF, \
    GATT_SERVICE_UUID_DEVICE_INFORMATION >> 8 \
};

static le_adv_item_handle gatt_server_dis_registered_handle = NULL;
static le_adv_item_data_t gatt_device_info_advert;

static bool gattServerDeviceInfo_GetItemData(le_adv_item_data_t * item)
{
    PanicNull(item);
    *item = gatt_device_info_advert;
    return TRUE;
}

static bool gattServerDeviceInfo_GetItemInfo(le_adv_item_info_t * info)
{
    PanicNull(info);
    *info = (le_adv_item_info_t){ .placement = le_adv_item_data_placement_advert,
#ifdef PREFER_LEGACY_ADVERTISING
                                    .type = le_adv_type_legacy_connectable_scannable,
#else
                                    .type = le_adv_type_extended_connectable,
#endif
                                    .data_size = gatt_device_info_advert.size };
    return TRUE;
}

le_adv_item_callback_t gattServerDeviceInfo_AdvertisingManagerCallback =
{
    .GetItemData = &gattServerDeviceInfo_GetItemData,
    .GetItemInfo = &gattServerDeviceInfo_GetItemInfo,
};

bool GattServerDis_SetupLeAdvertisingData(void)
{
    gatt_device_info_advert.size = SIZE_DEVICE_INFO_ADVERT;
    gatt_device_info_advert.data = gatt_device_info_advert_data;
    gatt_server_dis_registered_handle = LeAdvertisingManager_RegisterAdvertisingItemCallback(NULL, &gattServerDeviceInfo_AdvertisingManagerCallback);
    return (gatt_server_dis_registered_handle ? TRUE : FALSE);
}

bool GattServerDis_UpdateLeAdvertisingData(void)
{
    return (gatt_server_dis_registered_handle ? LeAdvertisingManager_UpdateAdvertisingItem(gatt_server_dis_registered_handle) : FALSE);
}
